//
//  LXBelongingCompanyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/1/27.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXBelongingCompanyViewController.h"
#import "LXTableViewCell.h"
#import "LXRemarkTabelViewCell.h"
#import "QiYeXinXiZhanShiVC.h"
#import "QiYeModel.h"

@interface LXBelongingCompanyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSString *introduction;
@property (nonatomic) QiYeModel *model;

@end

@implementation LXBelongingCompanyViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self layoutCustomNav:self.view title:@"所属企业"];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];

    [self loadData];
    
    [MSNetworkManager postRevertUrl:POST_Company_Detail_Url params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if(isSuccess){
            QiYeModel *model = [QiYeModel mj_objectWithKeyValues:resonseObj];
            self.model = model;
            self.introduction = model.introduction;
            [self.tableView reloadData];
        }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
    
    if ([kUserDefaultForKey(EduitString_key) isEqualToString:EduitString]) {
        [self setBottomViewWithText:@"保存"];
    }
   
}
- (void)loadData{
    
    
    self.data = @[
        
        @{
            @"title":@"当前企业",
            @"key":@"currentCompany",
            @"value":[YLUserMgr getEnterpriseName],
            @"canEdit":@(NO),
            @"detailTextColor":ColorWithAAAAAA,
            @"showAccess":@(NO),
            @"maxLength":@(1000),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"查看企业详细信息",
            @"key":@"lookCompany",
            @"placeHolder" : @"",
            @"canEdit":@(NO),
            @"showAccess":@(YES),
            @"maxLength":@(1000),
            @"sel":@"clickCompanyInfo",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"企业简介",
            @"key":@"introduction",
            @"height":@(180)
        },
        
        
    ].mutableCopy;
        
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.row];
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.row];

    // 简介
    if([dic[@"key"]  isEqualToString:@"introduction"]){
       LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.textView.placeholder = @"请输入 ~";
       cell.textView.maxLength = 100;
       cell.remarkTitle.text = dic[@"title"];
       cell.textView.text = self.introduction;
        cell.textView.backgroundColor = UIColor.whiteColor;
        cell.textView.layer.borderColor = [UIColor colorWithHexString:@"#D4D4D4"].CGColor;
        cell.textView.layer.borderWidth = 1;
        
        if ([kUserDefaultForKey(EduitString_key) isEqualToString:EduitString]) {
            cell.textView.userInteractionEnabled = YES;
        }
        else {
            cell.textView.userInteractionEnabled = NO;
            if (StringIsEmpty(self.introduction)) {
                cell.textView.text = @"暂无简介";
            }
            
        }
        
       [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
           self.introduction  = textStr;
       }];
       return cell;
   }
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.cellDic = dic;
    cell.textFiled.hidden = YES;
    cell.subTitleLab.hidden = NO;
    cell.subTitleLab.text = dic[@"value"];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.row];
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}

- (void)clickCompanyInfo{
    QiYeXinXiZhanShiVC *vc = [[QiYeXinXiZhanShiVC alloc] init];
    vc.detailModel = self.model;
    [self pushToNextController:vc];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, ScreenHeight - LL_StatusBarAndNavigationBarHeight - kComHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.contentInset = UIEdgeInsetsMake(0,0,kComHeight + 10,0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 
    }
    return _tableView;
}
- (void)clickBottomView:(UIButton *)sender{
    YLUserInfo *info = [YLUserMgr fetchUser];;
    NSDictionary  *dic = @{
        @"id":info.companyId,
        @"introduction":self.introduction
    };
    [MSNetworkManager postRevertUrl:POST_UpdateEnterInfo params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            showAlert(@"保存成功");
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
}
@end
